﻿using MvvmCross.Core.ViewModels;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Sidvall.WorkItemManager.UI.ViewModels
{
    public partial class UserConfigurationViewModel
    {
        #region Public Members

        #region PageTypes

        private Sidvall.Data.KeyValueCollection<string, Sidvall.WorkItemManager.Data.PageType> _PageTypes;
        public Sidvall.Data.KeyValueCollection<string, Sidvall.WorkItemManager.Data.PageType> PageTypes
        {
            get
            {
                if (_PageTypes == null)
                    this.PageTypes = Sidvall.Data.KeyValueManager.FromEnum<Sidvall.WorkItemManager.Data.PageType>();
                return _PageTypes;
            }
            set
            {
                _PageTypes = value;
                RaisePropertyChanged("PageTypes");
            }
        }

        #endregion
        #region CanConnectToServer

        public bool CanConnectToServer
        {
            get
            {
                return Sidvall.WorkItemManager.Security.SecurityManager.CanConnectToServer;
            }
        }

        #endregion

        // Commands
        #region ExportSettingsCommand

        public ICommand ExportSettingsCommand
        {
            get
            {
                return new MvxCommand(async () => await ExportSettingsAsync());
            }
        }

        #endregion
        #region ExportSettingsAsync

        public async Task ExportSettingsAsync()
        {
            if (this.ActiveEntity == null)
                return;
            var item = new Net.Services.V1.UserConfiguration();
            item.Import(this.ActiveEntity, Sidvall.Data.CopySettings.FullCopyAll(Sidvall.Data.DataRowContextOption.None, null));
            await ViewModelManager.ExportSettingsAsync("UserConfiguration.txt", item);
        }

        #endregion
        #region ImportSettingsCommand

        public ICommand ImportSettingsCommand
        {
            get
            {
                return new MvxCommand(async () => await ImportSettingsAsync());
            }
        }

        #endregion
        #region ImportSettingsAsync

        public async Task ImportSettingsAsync()
        {
            Task task = null;
            if (this.ActiveEntity == null)
                return;
            try
            {
                var content = await ViewModelManager.ImportSettingsAsync();
                if (string.IsNullOrWhiteSpace(content))
                    return;
                var item = Sidvall.WorkItemManager.UI.SystemContext.Current.LoadUserConfiguration(content);
                this.ActiveEntity = item.Copy(Sidvall.Data.CopySettings.FullCopyAll(Sidvall.Data.DataRowContextOption.StateAndKey, null));
                this.ActiveEntity = await Sidvall.WorkItemManager.UI.SystemContext.Current.SaveUserConfigurationAsync(this.ActiveEntity, true);
            }
            catch (System.Exception ex)
            {
                task = Sidvall.UI.SystemContext.Current.DialogManager.ShowErrorMessageAsync("Export Error", ex);
            }
            if (task != null)
                await task;
        }

        #endregion
        #region RefreshItemsCommand

        public ICommand RefreshItemsCommand
        {
            get
            {
                return new MvxCommand(async () => await RefreshItemsAsync());
            }
        }

        #endregion
        #region RefreshItemsAsync

        public async Task RefreshItemsAsync()
        {
            Task task = null;
            using (var busyManager = new Sidvall.Data.BusyManager(this))
            {
                try
                {
                    this.ActiveEntity = await Sidvall.WorkItemManager.UI.SystemContext.Current.LoadUserConfigurationAsync();
                }
                catch (System.Exception ex)
                {
                    task = Sidvall.UI.SystemContext.Current.DialogManager.ShowErrorMessageAsync("Export Error", ex);
                }
                if (task != null)
                    await task;
            }
        }

        #endregion
        #region ConnectToServer

        public ICommand ConnectToServerCommand
        {
            get
            {
                return new MvxCommand(async () => await ConnectToServerAsync());
            }
        }
        public async Task ConnectToServerAsync()
        {
            Task task = null;
            if (!this.CanConnectToServer)
                return;
            using (var busyManager = new Sidvall.Data.BusyManager(this))
            {
                try
                {
                    await Sidvall.WorkItemManager.UI.SystemContext.Current.LoadServerInfoAsync(true, true);
                    await DoLoadAsync();
                }
                catch (System.Exception ex)
                {
                    task = Sidvall.UI.SystemContext.Current.DialogManager.ShowErrorMessageAsync("Export Error", ex);
                }
                if (task != null)
                    await task;
            }
        }

        #endregion
        #region OpenFolder

        public ICommand OpenFolderCommand
        {
            get
            {
                return new MvxCommand(async () => await OpenFolderAsync());
            }
        }
        public async Task OpenFolderAsync()
        {
            Task task = null;
            string path;

            try
            {
                path = await Sidvall.UI.SystemContext.Current.DialogManager.OpenFolderAsync(Sidvall.UI.Dialogs.SpecialFolder.Desktop, this.ActiveEntity.PluginPath, true);
                if (!string.IsNullOrWhiteSpace(path))
                {
                    this.ActiveEntity.PluginPath = path;
                }
            }
            catch (System.Exception ex)
            {
                task = Sidvall.UI.SystemContext.Current.DialogManager.ShowErrorMessageAsync("Export Error", ex);
            }
            if (task != null)
                await task;
        }

        #endregion

        #region LoadItemAsync

        protected override Task<Entities.UserConfiguration> LoadItemAsync()
        {
            var item = Sidvall.WorkItemManager.UI.SystemContext.Current.UserConfiguration.Copy(Sidvall.Data.CopySettings.FullCopyAll(Sidvall.Data.DataRowContextOption.StateAndKey, null));
            return Task.FromResult(item);
        }

        #endregion
        #region SaveItemAsync

        protected override async Task<Entities.UserConfiguration> SaveItemAsync(Entities.UserConfiguration item)
        {
            item = await Sidvall.WorkItemManager.UI.SystemContext.Current.SaveUserConfigurationAsync(item, false);
            return item.Copy(Sidvall.Data.CopySettings.FullCopyAll(Sidvall.Data.DataRowContextOption.StateAndKey, null));
        }

        #endregion
        #region StartAsync

        protected override async Task StartAsync()
        {
            await DoLoadAsync();
        }

        #endregion

        #endregion
    }
}
